﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RN.ERP.Entity;
using RN.ERP.DAL;
using System.Configuration;
using Fly.FlyCommon;
using Fly.Log;
using RN.ERP.DAL.DAL;


namespace RN.ERP.BLL.Material
{

    public class MaterialClassBLL
    {
         private string _appId = ConfigurationManager.AppSettings["AppID"];

        private MaterialContext baseContext;

        private MaterialContext dbContext
        {
            get
            {
                return (MaterialContext)this.baseContext;
            }
        }

        public MaterialClassBLL()
        {
            this.baseContext = new MaterialContext();
        }

        public List<DTO.MateClass> GetList()
        {
            var query = from a in dbContext.DbMateClass
                        where a.iStatus == 0
                        select a;

            List<MateClass> list = query.OrderByDescending(m => m.dCDate).ToList();
            List<DTO.MateClass> lists = new List<DTO.MateClass>();
            foreach (MateClass item in list)
            {
                DTO.MateClass role = new DTO.MateClass();
                EntityHelper.ConvertEntity2OtherEntity(item, role);

                string cCUName = T_Sys_UsersDAL.GetNameByUserGuid(item.cCUID.ToString());

                string cUUName = T_Sys_UsersDAL.GetNameByUserGuid(item.cUUID.ToString());


                role.iStatus = item.iStatus == 0 ? "有效" : "无效";
                role.cCUName = cCUName;
                role.CUUName = cUUName;

                lists.Add(role);
            }
            return lists;
        }
        public List<DTO.MateClass> GetList(string cPMCCode)
        {
            var query = from a in dbContext.DbMateClass
                        where a.cPMCCode==cPMCCode&& a.iStatus == 0
                        select a;

            List<MateClass> list = query.OrderByDescending(m => m.dCDate).ToList();
            List<DTO.MateClass> lists = new List<DTO.MateClass>();
            foreach (MateClass item in list)
            {
                DTO.MateClass role = new DTO.MateClass();
                EntityHelper.ConvertEntity2OtherEntity(item, role);

                string cCUName = T_Sys_UsersDAL.GetNameByUserGuid(item.cCUID.ToString());

                string cUUName = T_Sys_UsersDAL.GetNameByUserGuid(item.cUUID.ToString());
                

                role.iStatus = item.iStatus == 0 ? "有效" : "无效";
                role.cCUName = cCUName;
                role.CUUName = cUUName;

                lists.Add(role);
            }
            return lists;
        }


        public DTO.OperationResult DeleteMaterialClass(string id)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            MateClass mProperty = dbContext.DbMateClass.SingleOrDefault(mp => mp.cMCCode == id && mp.iStatus == 0);
            if (mProperty != null)
            {
                mProperty.iStatus = 1;
                try
                {
                    if (dbContext.SaveChanges() > 0)
                    {
                        result.Result = true;
                        result.Message = "物资类别删除成功！";
                        UserLogManager.Log(new User_Log()
                        {
                            AppId = _appId,
                            Content = "删除物资类别",
                            FunctionId = FunctionId.MaterialProperty.ToString(),
                            FunctionType = FunctionType.Delete.ToString(),
                            IsrtDate = DateTime.Now,
                            IsrtUser = mProperty.cCUID.ToString()
                        });
                    }
                }
                catch (Exception ex)
                {
                    result.Message = "物资类别删除失败：" + ex.Message;
                }
            }
            else
            {
                result.Message = "未找到需要删除的物资类别！";
            }

            return result;
        }


        public DTO.OperationResult AddMaterialClass(DTO.MateClass property)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            MateClass mateClass = new MateClass();
            EntityHelper.ConvertEntity2OtherEntity<DTO.MateClass, MateClass>(property, mateClass);
            mateClass.dCDate = DateTime.Now;
            mateClass.cMCCode = new MaterialBLL().GetCode("B_MateClass", "cMCCode", 3);
            try
            {
                mateClass = dbContext.DbMateClass.Add(mateClass);
                if (dbContext.SaveChanges() > 0)
                {
                    result.Result = true;
                    result.Message = "物资类别添加成功！";
                    UserLogManager.Log(new User_Log()
                    {
                        AppId = _appId,
                        Content = "添加物资类别",
                        FunctionId = FunctionId.MaterialClass.ToString(),
                        FunctionType = FunctionType.Add.ToString(),
                        IsrtDate = DateTime.Now,
                        IsrtUser = mateClass.cCUID.ToString()
                    });
                }
            }
            catch (Exception ex)
            {
                result.Message = "物资类别添加失败：" + ex.Message;
            }

            return result;
        }

        public DTO.OperationResult EditMateClass(DTO.MateClass property)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            MateClass mateClass = dbContext.DbMateClass.SingleOrDefault(mp => mp.id == property.id && mp.iStatus == 0);
            if (mateClass == null)
            {
                result.Message = "无此物资类别信息！";
                return result;
            }
            mateClass.cMemo = property.cMemo;
            //mateClass.cMCCode = property.cMCCode;
            mateClass.cMCName = property.cMCName;
            mateClass.cPMCCode = property.cPMCCode;
            //mateClass.dCDate = property.dCDate;
            //mateClass.cCUID = property.cCUID;
            mateClass.cUUID = property.cUUID;
            mateClass.dUDate = DateTime.Now;
            mateClass.iStatus = int.Parse(property.iStatus);
            if (dbContext.SaveChanges() > 0)
            {
                result.Result = true;
                result.Message = "物资类别修改成功！";
                UserLogManager.Log(new User_Log()
                {
                    AppId = _appId,
                    Content = "修改物资类别",
                    FunctionId = FunctionId.MaterialClass.ToString(),
                    FunctionType = FunctionType.Edit.ToString(),
                    IsrtDate = DateTime.Now,
                    IsrtUser = mateClass.cCUID.ToString()
                });
            }

            return result;
        }

        public DTO.MateProperty GetMaterialProperty(int id)
        {
            MateProperty mProperty = dbContext.DbMateProperty.SingleOrDefault(mp => mp.id == id && mp.iStatus == 0);
            DTO.MateProperty property = new DTO.MateProperty();
            if (mProperty != null)
            {
                EntityHelper.ConvertEntity2OtherEntity<MateProperty, DTO.MateProperty>(mProperty, property);
                return property;
            }
            else
            {
                return null;
            }
        }
    }
}
